package com.itheima.controller;

import com.itheima.health.dto.SetmealDTO;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.service.CheckGroupService;
import com.itheima.health.service.SetMealService;
import com.itheima.health.utils.aliyunoss.AliyunUtils;
import com.itheima.health.utils.redis.RedisUtil;
import com.itheima.health.utils.resources.RedisConstant;
import com.itheima.health.utils.resources.UploadUtils;
import io.swagger.annotations.Api;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

@RestController
@Api(tags = "套餐管理开发")  // 作用在类上
public class SetMealController {

    @Reference
    private SetMealService setmealService;

    @Reference
    private CheckGroupService checkGroupService;

    @PostMapping("setmeal/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        return  new Result(setmealService.findPage(queryPageBean));
    }

    @PostMapping("setmeal/add")
    public Result  add(@RequestBody SetmealDTO setmealDTO){
        return  new Result(setmealService.saveSetMeal(setmealDTO));  //   业务代码实现
    }
    @PostMapping("setmeal/upload")
    public Result  upload(@RequestParam("imgFile")MultipartFile file){
        try {
            //  MultipartFile file  上传文件对象
            String originalFilename = file.getOriginalFilename();
            String uuidfilename = UploadUtils.generateRandonFileName(originalFilename);
            //  阿里云sdk
            AliyunUtils.uploadMultiPartFileToAliyun(file.getBytes(),uuidfilename);
            //   图片信息 保存  set 集合
            RedisUtil.addToSet(RedisConstant.ALL_SETMEAL_PIC_SET,uuidfilename);  //   图片存储到set集合
            //  图片存储到redis  保存10分钟   超过 10分 redis  自动删除该图片信息
            RedisUtil.set(RedisConstant.SINGLE_PIC+uuidfilename,uuidfilename,2, TimeUnit.MINUTES);
            return  new Result(uuidfilename);
        } catch (IOException e) {
            e.printStackTrace();
            throw  new RuntimeException(e.getMessage());
        }
        }
        @GetMapping("setmeal/findAll")
        public Result findAll(){
            return new Result(checkGroupService.list());
    }


}








